package com.ss.android.action;

import android.content.Context;
import android.os.Handler;

import com.bytedance.common.utility.Logger;
import com.ss.android.account.Spipe;
import com.ss.android.common.AbsApiThread;
import com.ss.android.common.applog.AppLog;
import com.ss.android.common.util.NetworkUtils;
import com.ss.android.model.ItemActionV3;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public final class ActionThreadV3 extends AbsApiThread {
    private Context mContext;
    private final Handler mHandler;
    private ItemActionV3 mItemActionV3;

    public ActionThreadV3(Context context, Handler handler, ItemActionV3 itemActionV3) {
        super("ActionThreadV3");
        this.mContext = context.getApplicationContext();
        this.mHandler = handler;
        this.mItemActionV3 = itemActionV3;
    }

    private boolean doSendActionsV3(JSONArray actions) {
        boolean v0 = false;
        if(this.mContext != null && NetworkUtils.isNetworkAvailable(this.mContext) && actions.length() > 0) {
            JSONObject jsonObject = new JSONObject();
            try {
                jsonObject.put("actions", actions);
                JSONObject timeSync = AppLog.f(this.mContext).timeSync;
                if(timeSync != null) {
                    jsonObject.put("time_sync", timeSync);
                }
                String v1_2 = NetworkUtils.executePost(
                        -1,
                        Spipe.userDataBatchActionUrl,
                        jsonObject.toString().getBytes("UTF-8"),
                        NetworkUtils.CompressType.GZIP,
                        "application/json; charset=utf-8");
                if(v1_2 == null) {
                    return v0;
                }

                if(v1_2.length() == 0) {
                    return v0;
                }

                v0 = isApiSuccess(new JSONObject(v1_2));
            }
            catch(Throwable v1_1) {
                Logger.e("ActionThreadV3", "throwable in doSendActionsV3 : " + v1_1.toString());
            }
        }

        return v0;
    }

    public final void run() {
        Logger.i("ActionThreadV3", "start ActionThreadV3");
        ItemActionV3 itemActionV3 = this.mItemActionV3;
        if(itemActionV3 != null && (itemActionV3.a())) {
            JSONArray ja = new JSONArray();
            JSONObject jo = new JSONObject();
            try {
                jo.put("action", itemActionV3.action);
                jo.put("type", itemActionV3.targetType);
                jo.put("id", itemActionV3.d.groupId);
                jo.put("item_id", itemActionV3.d.itemId);
                jo.put("aggr_type", itemActionV3.d.aggrType);
                jo.put("timestamp", itemActionV3.timestamp / 1000);
                if(itemActionV3.b()) {
                    jo.put("filter_words", itemActionV3.filterWords);
                }

                if(itemActionV3.c()) {
                    jo.put("extra", itemActionV3.extra);
                }

                if(itemActionV3.targetType == 3) {
                    jo.put("ad_id", itemActionV3.ad_id);
                    JSONObject adExtraJO = new JSONObject();
                    adExtraJO.put("clicked", itemActionV3.clicked);
                    adExtraJO.put("log_extra", itemActionV3.logExtra);
                    jo.put("ad_extra", adExtraJO);
                }

                ja.put(jo);
                int v0_2 = this.doSendActionsV3(ja) ? 1005 : 1006;
                if(this.mHandler == null) {
                    Logger.i("ActionThreadV3", "stop ActionThreadV3");
                }
                this.mHandler.sendMessage(this.mHandler.obtainMessage(v0_2, itemActionV3));
            } catch(JSONException jsonException) {
                Logger.e("ActionThreadV3", "exception in sendActionV3 : " + jsonException.toString());
                Logger.i("ActionThreadV3", "stop ActionThreadV3");
            }


        }
    }
}

